<template>
  <div>
    <div class="gva-form-box">
      <el-form :model="formData" label-position="right" label-width="80px">
        <el-form-item label="对应用户的id:">
          <el-input v-model.number="formData.commodity_user" clearable placeholder="请输入" />
        </el-form-item>
        <el-form-item label="商品名称:">
          <el-input v-model="formData.commodity_title" clearable placeholder="请输入" />
        </el-form-item>
        <el-form-item label="商品信息:">
          <el-input v-model="formData.commodity_text" clearable placeholder="请输入" />
        </el-form-item>
        <el-form-item label="价格:">
          <el-input-number v-model="formData.commodity_price" :precision="2" clearable></el-input-number>
        </el-form-item>
        <el-form-item label="配送范围:">
          <el-select v-model="formData.commodity_send" placeholder="请选择" clearable>
            <el-option v-for="(item,key) in positionOptions" :key="key" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="商品所在位置:">
          <el-select v-model="formData.commodity_position" placeholder="请选择" clearable>
            <el-option v-for="(item,key) in positionOptions" :key="key" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="商品类型:">
          <el-select v-model="formData.commodity_label" placeholder="请选择" clearable>
            <el-option v-for="(item,key) in labelOptions" :key="key" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button size="mini" type="primary" @click="save">保存</el-button>
          <el-button size="mini" type="primary" @click="back">返回</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Commodity'
}
</script>

<script setup>
import {
  createCommodity,
  updateCommodity,
  findCommodity
} from '@/api/commodity'

// 自动获取字典
import { getDictFunc } from '@/utils/format'
import { useRoute, useRouter } from "vue-router"
import { ElMessage } from 'element-plus'
import { ref } from 'vue'
const route = useRoute()
const router = useRouter()
const type = ref('')
const positionOptions = ref([])
const labelOptions = ref([])
const formData = ref({
        commodity_user: 0,
        commodity_title: '',
        commodity_text: '',
        commodity_price: 0,
        commodity_send: undefined,
        commodity_position: undefined,
        commodity_label: undefined,
        })

// 初始化方法
const init = async () => {
 // 建议通过url传参获取目标数据ID 调用 find方法进行查询数据操作 从而决定本页面是create还是update 以下为id作为url参数示例
    if (route.query.id) {
      const res = await findCommodity({ ID: route.query.id })
      if (res.code === 0) {
        formData.value = res.data.recommodity
        type.value = 'update'
      }
    } else {
      type.value = 'create'
    }
    positionOptions.value = await getDictFunc('position')
    labelOptions.value = await getDictFunc('label')
}

init()
// 保存按钮
const save = async() => {
      let res
      switch (type.value) {
        case 'create':
          res = await createCommodity(formData.value)
          break
        case 'update':
          res = await updateCommodity(formData.value)
          break
        default:
          res = await createCommodity(formData.value)
          break
      }
      if (res.code === 0) {
        ElMessage({
          type: 'success',
          message: '创建/更改成功'
        })
      }
}

// 返回按钮
const back = () => {
    router.go(-1)
}

</script>

<style>
</style>
